package com.fyh.service.impl;

import com.fyh.entity.common.Result;
import com.fyh.entity.job.JobPositionRequest;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.JobFavorite;
import com.fyh.entity.user.Vitae;
import com.fyh.feign.CandidateChatFeignClient;
import com.fyh.feign.CandidateJobFeignClient;
import com.fyh.feign.CandidateUserFeignClient;
import com.fyh.mapper.CandidateMapper;
import com.fyh.service.CandidateService;
import com.fyh.util.OssUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class CandidateServiceImpl implements CandidateService {

    @Qualifier("com.fyh.feign.CandidateJobFeignClient")
    @Autowired
    CandidateJobFeignClient candidateJobFeignClient;

    @Qualifier("com.fyh.feign.CandidateUserFeignClient")
    @Autowired
    CandidateUserFeignClient candidateUserFeignClient;
    @Qualifier("com.fyh.feign.CandidateChatFeignClient")
    @Autowired
    CandidateChatFeignClient candidateChatFeignClient;

    @Autowired
    CandidateMapper candidateMapper;

    /**
     * 新增用户简历
     * @param vitae
     */
    @Override
    public void insertVitae(Vitae vitae) {
        candidateMapper.insertVitae(vitae);
    }

    /**
     * 更新用户简历
     * @param vitae
     */
    @Override
    @Transactional
    public void updateVitae(Vitae vitae) {
        // 更新用户简历
        candidateMapper.updateVitae(vitae);
        // 更新用户技能标签
        candidateMapper.deleteTagByUserId(vitae.getUserId());
        for (String tagName : vitae.getTags()){
            candidateMapper.insertTag(vitae.getUserId(), tagName);
        }
    }

    /**
     * 获取用户简历
     * @param userId
     * @return
     */
    @Override
    public Vitae getUserVitae(Long userId) {
        // 获取用户简历
        Vitae userVitae = candidateMapper.getUserVitae(userId);
        // 获取用户技能标签
        userVitae.setTags(candidateMapper.getTagNameListByUserId(userId));
        return userVitae;
    }

    /**
     * 获取用户技能标签
     * @param userId
     * @return
     */
    @Override
    public List<String> getTagNameListByUserId(Long userId) {
        return candidateMapper.getTagNameListByUserId(userId);
    }

    /**
     * 上传简历文件
     * @param file
     * @return
     */
    @Override
    public String uploadResumeFile(MultipartFile file) {
        try {
            //获取文件原始名
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成新的文件名
            String fileName = UUID.randomUUID() + suffix;

            String path = OssUtil.uploadFile(fileName, file.getBytes());
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取收藏职位列表
     * @param userId
     * @return
     */
    @Override
    public List<SimpleJobPosition> getCollectSimpleJobList(Long userId) {
        // 获取收藏职位ID列表
        List<JobFavorite> jobFavoriteList = candidateMapper.getJobFavoriteList(userId);
        // 获取职位基础信息列表
        List<SimpleJobPosition> simpleJobPositionList = candidateJobFeignClient.getSimpleJobListByPositionId(jobFavoriteList).getData();
        return simpleJobPositionList;
    }

    /**
     * 收藏职位
     * @param positionId
     * @param userId
     */
    @Override
    public void collectJob(Long positionId, Long userId) {

        candidateMapper.collectJob(positionId, userId, LocalDateTime.now());
    }

    /**
     * 取消收藏职位
     * @param positionId
     * @param userId
     */
    @Override
    public void cancelCollectJob(Long positionId, Long userId) {
        candidateMapper.cancelCollectJob(positionId, userId);
    }

    /**
     * 获取职位收藏状态
     * @param positionId
     * @param userId
     * @return
     */
    @Override
    public Boolean getCollectExist(Long positionId, Long userId) {
        Boolean status = candidateMapper.getCollectExist(positionId, userId);
        return status;
    }

    /**
     * 获取职位是否投递
     * @param positionId
     * @param userId
     * @return
     */
    @Override
    public Boolean getDeliveryExist(Long positionId, Long userId) {
        Result<Boolean> exist = candidateJobFeignClient.getDeliveryExist(positionId, userId);
        return exist.getData();
    }

    /**
     * 投递简历
     * @param positionId
     * @param userId
     */
    @Override
    public void deliveryVitae(Long positionId, Long userId) {
        // 新增投递记录
        candidateJobFeignClient.deliveryVitae(positionId, userId);
        // 获取职位招聘者ID
        Long employerId = candidateJobFeignClient.getEmployerIdByPositionId(positionId).getData();
        // 发起投递消息通知（招聘者在线）
        candidateChatFeignClient.sendDeliveryMessage(positionId, employerId);
    }


}
